package com.keba.lamp.complaint.strategy.impl;

import com.keba.lamp.common.constant.AppendixType;
import com.keba.lamp.complaint.enumeration.DocTemplateTypeEnum;
import com.keba.lamp.complaint.enumeration.MimeEnum;
import com.keba.lamp.complaint.service.DocTemplateService;
import com.keba.lamp.complaint.strategy.AbstractCaseFileStrategy;
import com.keba.lamp.complaint.strategy.domain.CaseDownloadParam;
import com.keba.lamp.complaint.strategy.domain.CaseDownloadResult;
import com.keba.lamp.file.entity.File;
import com.keba.lamp.file.vo.param.FileUploadVO;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * 结算确认书文件下载
 */
@Slf4j
@RequiredArgsConstructor
@Service
@Getter
@Setter
public class SettleConfirmFileStrategy extends AbstractCaseFileStrategy {

    @Autowired
    private DocTemplateService docTemplateService;
    @Override
    public CaseDownloadResult exec(CaseDownloadParam param) throws Exception {
        List<Long> comFileList = docTemplateService.listComeFileByCaseIdAndBizType(param.getCaseId(), DocTemplateTypeEnum.SETTLE_CONFIRM.getDesc());

        if (CollectionUtils.isNotEmpty(comFileList)) {
            ByteArrayOutputStream outputStream = pdfCommonService.createFileZipByFileIdList(comFileList);
            FileUploadVO fileUploadVO = new FileUploadVO();
            fileUploadVO.setBizType(AppendixType.Download.DOWNLOAD__SETTLE_CONFIRM__FILE);
            fileUploadVO.setContentType(MimeEnum.ZIP.getContentType());
            String fileName = DocTemplateTypeEnum.SETTLE_CONFIRM.getLabel() + "等.zip";
            File file = fileContext.upload(outputStream.toByteArray(), fileName, fileUploadVO);
            return CaseDownloadResult.builder().url(file.getUrl()).build();
        } else {
            return CaseDownloadResult.builder().build();
        }

    }
}
